package com.helios.manager.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.helios.core.bean.Result;
import com.helios.core.service.ConfigService;
import com.helios.user.bean.Manager;
import com.helios.user.config.ConfigKey;
import com.helios.user.service.ManagerLogService;
import com.helios.user.service.ManagerService;

/**
 * 首页控制器
 * 
 * @author liyu
 */
@RestController
public class MainController {
    
    @Autowired
    public ManagerService managerService;
    
    @Autowired
    public ConfigService ConfigService;
    
    @Autowired
    public ManagerLogService managerLogService;
    
    /**
     * 管理端首页. 
     */
    @RequestMapping(value = {"/", "index.html"})
    public ModelAndView toIndex(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("main/index");
        modelAndView.addObject("admin", request.getSession().getAttribute("KEY_AUTHOR_SESSION"));
        return modelAndView;
    }
    
    /**
     * 设置界面 .
     */
    @RequestMapping(value = "config.html")
    public ModelAndView toConfig(){
        ModelAndView modelAndView = new ModelAndView("main/config");
        return modelAndView;
    }
    
    /**
     * 管理端开发日志界面.
     */
    @RequestMapping(value = "logs.html")
    public ModelAndView toLogs(){
        ModelAndView modelAndView = new ModelAndView("main/logs");
        return modelAndView;
    }
    
    /**
     * 管理端登陆界面.
     */
    @RequestMapping(value = "login.html")
    public ModelAndView toLogin(HttpServletRequest request){
        if(request.getSession().getAttribute("KEY_AUTHOR_SESSION") != null)
            return new ModelAndView("redirect:/index.html");
        else
            return new ModelAndView("main/login");
    }
    
    /**
     * 管理端登陆界面.
     */
    @RequestMapping(value = "404.html")
    public ModelAndView to404(HttpServletRequest request){
    	ModelAndView modelAndView = new ModelAndView("main/404");
    	return modelAndView;
    }
    
    /**
     * 检测是否登陆理端.
     */
    @RequestMapping(value = "islogin.json")
    public Result islogin(HttpServletRequest request){
        Result result = new Result();
        Object obj = request.getSession().getAttribute("KEY_AUTHOR_SESSION");
        if(obj != null && obj instanceof Manager){
            result.setCode(0);
            result.putInfo(System.currentTimeMillis() + "");
        }else{
            result.setCode(-1);
            result.putInfo("未登录");
        }
        return result;
    }
    
    /**
     * 登陆管理端.
     * 
     * 参数:
     * username 用户名
     * password 密码
     */
    @RequestMapping(value = "login.json")
    public Result login(HttpServletRequest request){
    	String logState = ConfigService.get(ConfigKey.MANAGER_LOG_STATE);
        Result result = new Result();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Manager manager = managerService.login(username, password);
        if(manager == null) {
            result.setCode(-1);
            result.putInfo("用户名或密码错误!");
            if(ConfigKey.STATE_ON.equalsIgnoreCase(logState)) {
            	managerLogService.save(0, username + ",登录失败", -1);
            }
        } else{
            request.getSession().setAttribute("KEY_AUTHOR_SESSION", manager);
            result.setCode(0);
            result.putInfo("");
            if(ConfigKey.STATE_ON.equalsIgnoreCase(logState)) {
            	managerLogService.save(manager.getId(), username + ",登录系统", 0);
            }
        }
        return result;
    }
    
    /**
     * 退出管理端.
     */
    @RequestMapping(value = "logout.json")
    public Result logout(HttpServletRequest request){
        request.getSession().removeAttribute("KEY_AUTHOR_SESSION");
        Result result = new Result();
        result.setCode(0);
        result.putInfo("");
        return result;
    }
}
